package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjCustomer;
import com.zb.entity.ZjCustomerInfo;
import com.zb.enums.CustomerStatus;
import com.zb.mapper.ZjCustomerMapper;
import com.zb.service.ZjCustomerInfoService;
import com.zb.service.ZjCustomerService;
import com.zb.service.ZjEmpService;
import com.zb.service.ZjRoleService;
import com.zb.utils.RandomUtil;
import com.zb.vo.CustomerInfoUpdateVo;
import com.zb.vo.CustomerInfoVo;
import com.zb.vo.CustomerRegisterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 商家表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-18
 */
@Service
public class ZjCustomerServiceImpl extends ServiceImpl<ZjCustomerMapper, ZjCustomer> implements ZjCustomerService {
    @Resource
    private ZjCustomerMapper customerMapper;
    @Resource
    private ZjCustomerInfoService customerInfoService;
    @Resource
    private ZjEmpService empService;
    @Resource
    private ZjRoleService roleService;

    // 注册企业
    @Override
    @Transactional
    public void register(AccounLoginInfo info, CustomerRegisterVo registerVo) {
        Assert.isTrue(null == customerMapper.existByFullName(registerVo.getName()), "企业名称已被注册", registerVo.getName());

        ZjCustomer customer = CustomerRegisterVo.voToBean(registerVo);
        customer.setId(IdWorker.getId());
        customer.setCode(RandomUtil.random60());
        customer.setStatus(CustomerStatus.WAIT_AUDIT.getType());
        customer.setLogoCode(RandomUtil.random10());
        customer.setAnnexCode(RandomUtil.random10());
        customer.setVideoCode(RandomUtil.random10());
        this.save(customer);
        customerInfoService.save(customer.getId());
        empService.saveSupperAccount(customer.getId(), info.getId());
        roleService.saveAdminRole(customer.getId(), info.getId());
    }

    // 获取企业信息
    @Override
    public CustomerInfoVo getCustomerInfo(Long id) {
        ZjCustomer customer = this.getById(id);
        CustomerInfoVo vo = CustomerInfoVo.beanToVo(customer);
        if (vo.getCode().length() > 6) {
            vo.setCode(vo.getCode().substring(0, 6) + "******" + vo.getCode().substring(vo.getCode().length() - 6));
        } else {
            vo.setCode(vo.getCode().substring(0, 1) + "******" + vo.getCode().substring(vo.getCode().length() - 1));
        }
        if (vo.getAnnexCode().length() > 6) {
            vo.setAnnexCode(vo.getAnnexCode().substring(0, 6) + "******" + vo.getAnnexCode().substring(vo.getAnnexCode().length() -6));
        } else {
            vo.setAnnexCode(vo.getAnnexCode().substring(0, 1) + "******" + vo.getAnnexCode().substring(vo.getAnnexCode().length() -1));
        }
        if (vo.getVideoCode().length() > 6) {
            vo.setVideoCode(vo.getVideoCode().substring(0, 6) + "******" + vo.getVideoCode().substring(vo.getVideoCode().length() -6));
        } else {
            vo.setVideoCode(vo.getVideoCode().substring(0, 1) + "******" + vo.getVideoCode().substring(vo.getVideoCode().length() -1));
        }
        ZjCustomerInfo info = customerInfoService.getById(id);
        BeanUtils.copyProperties(null == info? new ZjCustomerInfo() : info, vo);
        return vo;
    }

    // 根据企业ID获取企业代码
    @Override
    public String getCustomerCode(Long id) {
        QueryWrapper<ZjCustomer> wrapper = new QueryWrapper<>();
        wrapper.select("code");
        wrapper.eq("id", id);
        ZjCustomer customer = this.getOne(wrapper);
        Assert.isTrue(null != customer, "企业数据不存在！");
        return customer.getCode();
    }

    // 修改企业信息
    @Override
    public void updateCustomer(Long custAccId, CustomerInfoUpdateVo updateVo) {
        ZjCustomer customer = new ZjCustomer();
        BeanUtil.copyProperties(updateVo, customer);
        customer.setId(custAccId);
        customer.setFullName(updateVo.getName());
        this.updateById(customer);
    }
}
